<?php
// +----------------------------------------------------------------------
// | 公共元素
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: PublicAction.class.php 122 2011-05-18 09:00:37Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class PublicAction extends Action {

	//初始化接口
	public function _initialize() {
		
		/* 对用户传入的变量进行去除转义操作。*/
		all_stripslashes();
			
		/* 载入系统参数 */
		global $_CFG; 
		$_CFG = get_config();	
		$this->assign('_CFG',$_CFG);
		
		/* 创建静态缓存文件 */
		import("@.ORG.StaticCaches");
		$cache = new StaticCaches();
		$cache->cacheInit();
		//create_static_caches();
		
		header('Expires: Fri, 14 Mar 1980 20:53:00 GMT');
		header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
		header('Cache-Control: no-cache, must-revalidate');
		header('Pragma: no-cache');		
	}
	
	//头部
	public function header() {
		C('TOKEN_ON',false);
	}

	//尾部
	public function footer() {
	}
	
	//关于
	public function sidebar() {
	
	    /* 获取已激活的小工具 */
		//$widget_on = (C('on_widget')) ? unserialize(C('on_widget')) : array();
//		/* 获取已激活的自定义小工具 */
//		$widget_custom  =  unserialize(C('custom_widget'));
//		
//		$sidebar_output = '';
//		
//		foreach ($widget_on as $val) {
//			if(strpos($val, 'Custom') === 0) {
//				$sidebar_output .= W('Custom',array('title'=>htmlspecialchars($widget_custom[$val]['title']), 'description'=>$widget_custom[$val]['description']),true);
//			}else{
//				$sidebar_output .= W(htmlspecialchars($val),'',true);
//			}
//		}
//		$this->assign('sidebar_output', $sidebar_output);
	}	
	
	public function allLinks() {
		$links = M('Links');
		$map = array();
		$map['link_status'] = 1; 
		$map['link_visible'] = 1;
		$data = $links->field('link_url,link_name,link_description')->where($map)->order('link_sort asc')->findAll();
		foreach ($data as $v) {
			if ( C('links_columns') == 1 ) {
				$result .= '<li class="ml_item"><a href="' .$v['link_url'].'" rel="external" title="' .$v['link_description'].'" style="background:url(http://www.google.com/s2/favicons?domain='.$this->replaceHttp($v['link_url']).') no-repeat;">' .$v['link_name'].'</a></li>';
			} else {
				if ( $count % C('links_columns') == 0 ) {
					$result .= '<li class="ml_item"><div>';
				}
				$result .= '<div class="ml_col ml_col_' . C('links_columns') . '"><a href="' .$v['link_url'].'" rel="external" title="' .$v['link_description'].'" style="background:url(http://www.google.com/s2/favicons?domain='.$this->replaceHttp($v['link_url']).') no-repeat;">' .$v['link_name'].'</a></div>';
				++$count;
				if ( $count % C('links_columns') == 0 ) {
					$result .= '<div class="ml_fixed"></div></div></li>';
				}
			}
		}
		if ( $count % C('links_columns') != 0 ) {
			$result .= '<div class="ml_fixed"></div></div></li>';
		}
		echo $result;
	}
	
	protected function replaceHttp($str) {
		if (strpos($str, 'http://') === false && strpos($str, 'https://') === false) {
			return "";
		} else {
		    return preg_replace("/https?:\/\/|\//","",$str);
		}	
	}
	
	public function getCommentByPostsId($id) {
		//查询过滤
		$map  =	 array();
		$map['posts_id'] = $id;
		$map['comment_visible'] = 1;
		$map['comment_status'] = 1;
		if (C('remark')) {
			$map['comment_remark'] = 1;
		}
		
		$com  =  M ('Comment');
		import("@.ORG.Page");
		C ( 'PAGE_ROLLPAGE',C('page_roll') );
		$count = $com->where($map)->count();
		$p = new Page ($count, C('comment_num'));
		$list = $com->where($map)->limit($p->firstRow.','.$p->listRows)->order('comment_id desc')->findAll();
		
		foreach ($list as $k=>$v) {
			$list[$k]['comment_email'] = get_gravatar($list[$k]['comment_email'], $s = 80, $d = C('avatar_default'), $r = C('avatar_rating'));
			$list[$k]['comment_addtime'] = date('F jS, Y', $list[$k]['comment_addtime']).' at '.date('G:i', $list[$k]['comment_addtime']);
			$list[$k]['comment_content'] = convert_smilies($list[$k]['comment_content']);		
			if ($list[$k]['comment_recontent']) {
				$list[$k]['comment_retime'] = date('F jS, Y', $list[$k]['comment_retime']).' at '.date('G:i', $list[$k]['comment_retime']);
				$list[$k]['comment_recontent'] = convert_smilies($list[$k]['comment_recontent']);
			}
		}
		
		$p->setConfig('theme',C('index_page_theme'));
		$page = $p->show ();
		
		$this->assign('comment_author', Cookie::get('comment_author_'.C('COOKIEHASH')));
		$this->assign('comment_email', Cookie::get('comment_email_'.C('COOKIEHASH')));
		$this->assign('comment_url', Cookie::get('comment_url_'.C('COOKIEHASH')));
		$this->assign('page', $page);
		$this->assign('commentslist', $list);
	}
	
	public function getCommentByType($int){
        	
		//查询过滤
		$map  =	 array();
		$map['comment_type'] = $int;
		$map['comment_visible'] = 1;
		$map['comment_status'] = 1;
		if (C('remark')) {
			$map['comment_remark'] = 1;
		}
		
		$com  =  M ('Comment');
		import("@.ORG.Page");
		C ( 'PAGE_ROLLPAGE',C('page_roll') );
		C ('VAR_PAGE','c');
		$count = $com->where($map)->count();
		$p = new Page ($count, C('comment_num'));
		$list = $com->where($map)->limit($p->firstRow.','.$p->listRows)->order('comment_id desc')->findAll();
		
		foreach ($list as $k=>$v) {
			$list[$k]['comment_email'] = get_gravatar($list[$k]['comment_email'], $s = 80, $d = C('avatar_default'), $r = C('avatar_rating'));
			$list[$k]['comment_addtime'] = date('F jS, Y', $list[$k]['comment_addtime']).' at '.date('G:i', $list[$k]['comment_addtime']);
			$list[$k]['comment_content'] = convert_smilies($list[$k]['comment_content']);		
			if ($list[$k]['comment_recontent']) {
				$list[$k]['comment_retime'] = date('F jS, Y', $list[$k]['comment_retime']).' at '.date('G:i', $list[$k]['comment_retime']);
				$list[$k]['comment_recontent'] = convert_smilies($list[$k]['comment_recontent']);
			}
		}
		
		$p->setConfig('theme',C('index_page_theme'));
		$page = $p->show ();
		
		$this->assign('comment_author', Cookie::get('comment_author_'.C('COOKIEHASH')));
		$this->assign('comment_email', Cookie::get('comment_email_'.C('COOKIEHASH')));
		$this->assign('comment_url', Cookie::get('comment_url_'.C('COOKIEHASH')));
		$this->assign('page', $page);
		$this->assign('commentslist', $list);
    }
	
}

?>
